package com.db.httpserver;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public final class WorkerThread implements Runnable {
    private Socket socket;
        
    public WorkerThread(Socket socket) {
        this.socket = socket;
    }

    public static void startPool(int suggestedPort) throws IOException {
        Parameter.setPort(suggestedPort);
        Parameter.print();
        int port = Parameter.getPort();
        int workers = Parameter.getWorkers();
        int timeout = Parameter.getTimeout(); 
        
        ExecutorService executor = Executors.newFixedThreadPool(workers);
        ServerSocket serverSocket = new ServerSocket(port);
//        serverSocket.setSoTimeout(timeout);
        while (true) {
            Socket clientSocket = serverSocket.accept();
            clientSocket.setSoTimeout(timeout);
            executor.submit(new WorkerThread(clientSocket));
        }
        // create pool of worker threads
        // create server socket
        // accept connections
    }

    public void run() {
    	Worker worker = new Worker();
    	try {
            worker.handleRequest(socket);
        } catch (IOException e) {
            e.printStackTrace();
        }
    	// Worker.handleRequest(Socket)
    }
}